package com.zby.lease.web.admin.service.imp;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.lease.model.enums.ItemType;
import com.zby.lease.model.pojo.*;
import com.zby.lease.web.admin.mapper.*;
import com.zby.lease.web.admin.service.*;
import com.zby.lease.web.admin.vo.attr.AttrValueVo;
import com.zby.lease.web.admin.vo.graph.GraphVo;
import com.zby.lease.web.admin.vo.room.RoomDetailVo;
import com.zby.lease.web.admin.vo.room.RoomItemVo;
import com.zby.lease.web.admin.vo.room.RoomQueryVo;
import com.zby.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLabelService    roomLabelService;
    @Autowired
    private RoomPaymentTypeService  paymentTypeService;
    @Autowired
    private RoomLeaseTermService leaseTermService;
    @Autowired
    private RoomInfoMapper baseMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GeneralService generalService;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        super.saveOrUpdate(roomSubmitVo);
        if (id != null){
            generalService.removeRoomSumById(id);
        }
        //插入图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
        // 批量插入,创建数组
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
//            遍历图标视图列表进行插入
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }

        // 保存到数据库中
            graphInfoService.saveBatch(graphInfoList);
        }
        //2.保存新的roomAttrValueList
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue =   RoomAttrValue
                        .builder()
                        .roomId(roomSubmitVo.getId())
                        .attrValueId(attrValueId)
                        .build();
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }
        //3.保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility
                        .builder()
                        .roomId(roomSubmitVo.getId())
                        .facilityId(facilityInfoId)
                        .build();
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }
        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            ArrayList<RoomLabel> roomLabels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel
                        .builder()
                        .roomId(roomSubmitVo.getId())
                        .labelId(labelInfoId)
                        .build();
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }
        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            ArrayList<RoomPaymentType> paymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType paymentType = RoomPaymentType
                        .builder()
                        .roomId(roomSubmitVo.getId())
                        .paymentTypeId(paymentTypeId)
                        .build();
                paymentTypes.add(paymentType);
            }
            paymentTypeService.saveBatch(paymentTypes);
        }
        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> leaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm leaseTerm = RoomLeaseTerm
                        .builder()
                        .roomId(roomSubmitVo.getId())
                        .leaseTermId(leaseTermId)
                        .build();
                leaseTerms.add(leaseTerm);
            }
            leaseTermService.saveBatch(leaseTerms);
        }
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {


        return baseMapper.pageRoomItemByQuery(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomInfo roomInfo = baseMapper.selectById(id);
        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //3.查询graphInfoList
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);

        //4.查询attrValueList
        List<AttrValueVo> attrvalueVoList = attrValueMapper.selectListByRoomId(id);

        //5.查询facilityInfoList
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

        //6.查询labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

        //7.查询paymentTypeList
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

        //8.查询leaseTermList
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        // 创建RoomDetailVo对象，用于综合展示房间详细信息
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        // 将房间信息的属性复制到房间详细视图对象中
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        // 设置公寓信息，为房间详细视图添加公寓相关 context
        roomDetailVo.setApartmentInfo(apartmentInfo);

        // 设置图表信息列表，用于展示房间相关的图表数据
        roomDetailVo.setGraphVoList(graphVoList);

        // 设置属性值列表，用于展示房间的各种属性
        roomDetailVo.setAttrValueVoList(attrvalueVoList);

        // 设置设施信息列表，详细列出房间内提供的设施
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        // 设置标签信息列表，用于标记房间的特性或类别
        roomDetailVo.setLabelInfoList(labelInfoList);

        // 设置支付类型列表，告知用户可用的支付方式
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        // 设置租赁期限列表，明确房间可选的租赁期限
        roomDetailVo.setLeaseTermList(leaseTermList);


//     
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        // 根据ID移除房间信息及其相关关联信息
        // 首先移除房间本身的信息
        super.removeById(id);
//       删除房间汇总信息
        generalService.removeRoomSumById(id);


    }


}





